package com.example.learnspring.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtil {
    static final private String mySecretKey = "1a2b3c4d5e6f7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7e8f9a0b1c2";
    static public String generateToken(Map<String, Object> claims) {
        Key key = Keys.hmacShaKeyFor(mySecretKey.getBytes());
        return Jwts.builder().signWith(key, SignatureAlgorithm.HS256).addClaims(claims).setExpiration(new Date(System.currentTimeMillis()+12*3600*1000)).compact();
    }

    static public Claims getClaimsFromToken(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(mySecretKey.getBytes()) // 指定密钥
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
